/*
 * Project: SmartWard
 * Package: com.yesoft.ward.basic.client
 * File: BaseDBService
 * Date: 2019/4/25 22:29
 * Version: V1.0
 * Copyright © 2018 - 2019 YESOFT CHINA, Inc. All Rights Reserved.
 */
package com.ruoyi.ichen.base.service;

import com.ruoyi.ichen.base.exception.WeServiceException;
import com.ruoyi.ichen.base.mapper.BaseDBMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import javax.xml.ws.WebServiceException;
import java.io.Serializable;
import java.util.List;

/**
 * @ClassName: BaseDBService
 * @Description: 业务层数据查询基类
 * @Author Jun
 * @Date: 2019/4/25 22:29
 * @Version: V1.0
 */

public class BaseDBService<T>  extends BaseService{
    protected static final String PARAMETER_NULL = "传入参数是空的";
    protected static final String MANY_RESULT = "查询一条记录但是找到多条存在";

    @Autowired
    private BaseDBMapper<T> baseDBMapper;

    /**
     * 根据ID查找
     * @param id
     * @return
     */
    public T getById(Serializable id) {
        if(null == id)
            throw new WeServiceException(HttpStatus.NOT_ACCEPTABLE, PARAMETER_NULL);
        return baseDBMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询一条记录，如果存在多条记录，抛出异常
     * @param t
     * @return
     */
    public T getOne(T t) {
        if(null == t)
            throw new WeServiceException(HttpStatus.NOT_ACCEPTABLE, PARAMETER_NULL);
        try{
            return baseDBMapper.selectOne(t);
        }catch (Exception e) {
            //throw new WebServiceException(MANY_RESULT);
            throw new WebServiceException(e);
        }
    }

    /**
     * 根据实体中的属性查询总数，查询条件使用等号
     * @param t
     * @return
     */
    public int getCount(T t) {
        if(null == t)
            throw new WeServiceException(HttpStatus.NOT_ACCEPTABLE, PARAMETER_NULL);
        return baseDBMapper.selectCount(t);
    }

    /**
     * 根据条件查询总数，查询条件使用等号
     * @param example
     * @return
     */
    public int getCountByCondition(Example example) {
        if(null == example)
            throw new WeServiceException(HttpStatus.NOT_ACCEPTABLE, PARAMETER_NULL);
        return baseDBMapper.selectCountByExample(example);
    }

    /**
     * 查询全部结果，select(null)方法能达到同样的效果
     * @return
     */
    public List<T> findAll() {
        return baseDBMapper.selectAll();
    }

    /**
     * 根据实体类查找
     * @param t
     * @return
     */
    public List<T> findByEntity(T t) {
        if(null == t)
            throw new WeServiceException(HttpStatus.NOT_ACCEPTABLE, PARAMETER_NULL);
        return baseDBMapper.select(t);
    }

    /**
     * 根据条件查询
     * @param example
     * @return
     */
    public List<T> findByCondition(Example example) {
        if(null == example)
            throw new WeServiceException(HttpStatus.NOT_ACCEPTABLE, PARAMETER_NULL);
        return baseDBMapper.selectByExample(example);
    }

    /**
     * 保存一个实体，null属性也会保存
     * @param t
     * @return
     */
    public int insert(T t) {
        if(null == t)
            throw new WeServiceException(HttpStatus.NOT_ACCEPTABLE, PARAMETER_NULL);
        return baseDBMapper.insert(t);
    }

    /**
     * 保存一个实体，null属性不会保存
     * @param t
     * @return
     */
    public int insertSelective(T t) {
        if(null == t)
            throw new WeServiceException(HttpStatus.NOT_ACCEPTABLE, PARAMETER_NULL);
        return baseDBMapper.insertSelective(t);
    }

    /**
     * 根据属性跟新属性值不为NULL的值
     * @param t
     * @return
     */
    public int updateByIDSelective(T t) {
        if(null == t)
            throw new WeServiceException(HttpStatus.NOT_ACCEPTABLE, PARAMETER_NULL);
        return baseDBMapper.updateByPrimaryKeySelective(t);
    }

    /**
     * 根据实体主键删除
     * @param id
     * @return
     */
    public int deleteById(Serializable id) {
        if(null == id)
            throw new WeServiceException(HttpStatus.NOT_ACCEPTABLE, PARAMETER_NULL);
        return baseDBMapper.deleteByPrimaryKey(id);
    }

    /**
     * 根据实体属性作为条件进行删除，查询条件使用等号
     * @param t
     * @return
     */
    public int delete(T t) {
        if(null == t)
            throw new WeServiceException(HttpStatus.NOT_ACCEPTABLE, PARAMETER_NULL);
        return baseDBMapper.delete(t);
    }
}